package com.guxingke.typecho4j.widget.handle.meta;

import com.guxingke.typecho4j.Typecho4jException;
import com.guxingke.typecho4j.jfinal.model.Meta;
import com.guxingke.typecho4j.route.DispatcherStrategy;
import com.guxingke.typecho4j.route.Route;
import com.guxingke.typecho4j.route.Routes;
import com.guxingke.typecho4j.widget.WidgetHandle;
import com.guxingke.typecho4j.widget.provider.MetaBaseWidget;
import com.guxingke.typecho4j.widget.provider.ProviderWidget;
import com.guxingke.typecho4j.widget.provider.meta.tagsManageWidget;
import com.jfinal.core.Controller;

public class TagDeleteHandleWidget extends MetaBaseWidget implements
		WidgetHandle {

	@Override
	public void configRoute(Routes routes) {
		routes.addRoute(new Route("deleteTag", this.getClass(),
				"/admin/manage-tags",
				DispatcherStrategy.REDIRECT));
	}

	@Override
	public void handle(Controller controller) throws Typecho4jException {

		String[] mids = controller.getParaValues("mid[]");

		if (mids != null) {
			Long mid = null;
			for (String string : mids) {
				mid = Long.parseLong(string);

				Meta.dao.deleteTagById(mid);
			}
		}

		ProviderWidget.refreshProviderWidget(tagsManageWidget.class);
	}
}
